package com.qf.fmmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.fmmall.dao.ProductCommentsMapper;
import com.qf.fmmall.entity.ProductCommentsVo;
import com.qf.fmmall.service.ProductCommentsService;
import com.qf.fmmall.vo.PageVo;
import com.qf.fmmall.vo.ResStatus;
import com.qf.fmmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductCommentsServiceImpl implements ProductCommentsService {

    @Autowired
    private ProductCommentsMapper productCommentsMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo detailCommonts(Integer productId, Integer pageNum, Integer limit) {
        ResultVo vo = new ResultVo();
        PageHelper.startPage(pageNum,limit);

        List<ProductCommentsVo> productCommentsVoList=productCommentsMapper.detailCommonts(productId);

        PageInfo<ProductCommentsVo> productCommentsVoPageInfo = new PageInfo<>(productCommentsVoList);

        PageVo<ProductCommentsVo> pageVo = new PageVo<>();
        pageVo.setCount((int) productCommentsVoPageInfo.getTotal());//总条数
        pageVo.setPageCount(productCommentsVoPageInfo.getPages());//总页数
        pageVo.setList(productCommentsVoList);
        if (productCommentsVoList.size()==0){
            vo.setCode(ResStatus.NO);
            vo.setMsg("没有查询到商品的评论信息");
        }else {
            vo.setCode(ResStatus.OK);
            vo.setMsg("success");
            vo.setData(pageVo);
        }

        return vo;
    }
}
